﻿--[[
    @brief    安全检测 & 登录中
    @author    jameszhai
    @date    2017-05-19
--]]

ELoadingView = 
{
    sLoading  = "pnlLoading";

    sChecking = "pnlSecCheck";
    sLblIcon  = "pnlAni";
    sPrgCheck = "lvCheck";

    sLogining = "pnlLogining";
    sStatus   = "lblStatus";

       sLoadAni  = "client_login\\loading\\loading2.tfc";

};

TLoadingView =
{
    InitView = function(nRoot)
        TLoadingView.dwRootID = nRoot;
    end;

    UnInitView = function()
        TLoadingView.dwRootID  = 0;
        TLoadingView.pPnlView  = nil;

        TLoadingView.nProgress = 0;            -- 动画进度

        -- 安全检测
        TLoadingView.pChkView  = nil;
        TLoadingView.pLblIcon  = nil;        -- Icon
        TLoadingView.pLvCheck  = nil;        -- 检测进度

        -- 加载界面
        TLoadingView.pLoadView = nil;

        -- 状态
        TLoadingView.pLblState = nil;
    end;

    CheckUI = function()
        if nil ~= TLoadingView.pPnlView then
            return true;
        end;

        local     nRoot = ui_utility.get_ctrl_id_by_path(
                            TLoadingView.dwRootID, 
                            ELoadingView.sLoading);

        if nil == nRoot or 0 == nRoot then
            TGPLog.LogE("Loading", "Get pannel fail.");
            return false;
        end;

        TLoadingView.pPnlView = ui_utility.get_ctrl_ptr_by_id(nRoot);

        if nil == TLoadingView.pPnlView then
            TGPLog.LogE("Loading", "Get page fail.");
            return false;
        end;

        TLoadingView.pLoadView = ui_utility.get_ctrl_ptr_by_path(nRoot, 
                                    ELoadingView.sLogining);
        TLoadingView.pChkView  = ui_utility.get_ctrl_ptr_by_path(nRoot,
                                    ELoadingView.sChecking);
        TLoadingView.pLblState = ui_utility.get_ctrl_ptr_by_path(nRoot,
                                    ELoadingView.sStatus);

        if nil == TLoadingView.pLoadView or nil == TLoadingView.pChkView or
           nil == TLoadingView.pLblState then
            TGPLog.LogE("Loading", "Create loading view fail.");
            return false;
        end;

        local     nViewID = TLoadingView.pChkView:GetControlID();

        TLoadingView.pLblIcon = ui_utility.get_ctrl_ptr_by_path(nViewID, 
                                    ELoadingView.sLblIcon);
        TLoadingView.pLvCheck = ui_utility.get_ctrl_ptr_by_path(nViewID, 
                                    ELoadingView.sPrgCheck);

        if nil == TLoadingView.pLblIcon or nil == TLoadingView.pLvCheck then
            TGPLog.LogE("Loading", "Get sub control fail.");
            return false;
        end;

        TLoadingView.pLvCheck:SetRange(0, 100);

        return true;
    end;

    ShowView = function(bLoading)
        if not TLoadingView.CheckUI() then
            TGPLog.LogE("Loading", "Check UI fail.");
            return;
        end;
        if TLoginView ~= nil then
            TLoginView.ShowCapsTips(false);
        end;

        if bLoading then
            if nil ~= TLoadingView.pChkView then
                TLoadingView.pChkView:Show(FALSE);
            else
                TGPLog.LogE("Loading", "Checking view is nil.");
            end;

            if nil ~= TLoadingView.pLoadView then
                TLoadingView.pLoadView:Show(TRUE);
            else
                TGPLog.LogE("Loading", "Loading view is null.");
            end;
        else
            if nil ~= TLoadingView.pChkView then
                TLoadingView.pChkView:Show(TRUE);
            else
                TGPLog.LogE("Loading", "Checking view is null.");
            end;

            if nil ~= TLoadingView.pLoadView then
                TLoadingView.pLoadView:Show(FALSE);
            else
                TGPLog.LogE("Loading", "Loading view is null.");
            end;
            
            TLoadingView.nProgress = 0;
            TLoadingView.StartTimer();
        end;

        if nil ~= TLoadingView.pPnlView then
            TLoadingView.pPnlView:Show(TRUE);
            TLoadingView.bVisible  = true;
        else
            TGPLog.LogE("Loading", "Show view, loading pannel is nil.");
        end;
    end;

    HideView = function()
        TLoadingView.StopTimer();

        if nil ~= TLoadingView.pPnlView then
            TLoadingView.pPnlView:Show(FALSE);
            TLoadingView.UnInitView();
            TLoadingView.bVisible = false;
        else
            TGPLog.LogE("Loading", "Hide view, loading pannel is nil.");
        end;
    end;

    -- 启动扫描检测定时器
    StartTimer = function()
        if TLoadingView.bStartTmr then
            return;
        end;

        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddInterval("TLoadingView.OnScanTimer", 100, 0);
        else
            TGPLog.LogE("LoginV", "Add interval.");
        end;

        TLoadingView.bStartTmr = true;
    end;

    -- 关闭扫描动画定时器
    StopTimer = function()
        if TLoadingView.bStartTmr then
            local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveInterval("TLoadingView.OnScanTimer", 0);
            else
                TGPLog.LogE("LoginV", "Add interval.");
            end;

            TLoadingView.bStartTmr = false;
        end;
    end;

    -- 安全扫描动画定时器
    OnScanTimer = function()
        TLoadingView.nProgress = TLoadingView.nProgress + 20;    
        TLoadingView.ShowProgress(TLoadingView.nProgress);

        if TLoadingView.nProgress >= 100 then
            TLoadingView.StopTimer();
            comm_center.send_broadcast("Msg_OnSecScanEnd", {});
        end;
    end;

    IsVisible = function()
        return TLoadingView.bVisible;
    end;

    -- 显示进度
    ShowProgress = function(nProg)
        if (nil ~= TLoadingView.pLvCheck) then
            TLoadingView.pLvCheck:SetPos(nProg, TRUE);
        else
            TGPLog.LogE("Loading", "Set progress:" .. tostring(nProg));
        end;
    end;

    -- 显示状态
    SetStatus = function(zStatus)
        if nil == zStatus or nil == zStatus["state"] then
            TGPLog.LogE("Loading", "Set status, param is null.");
            return;
        end;

        if (nil ~= TLoadingView.pLblState) then
            ui_utility.setproperty_by_id(TLoadingView.pLblState:GetControlID(), 
                                         "Caption", zStatus["state"]);
        else
            TGPLog.LogE("Loading", "TLoadingView.pLblState is nil.");
        end;
    end;

    -- 动画进度
    OnAnimationStep = function(pUILib, dwCtrlID, bLoop, nFrame)
        --[[
        TLoadingView.nProgress = TLoadingView.nProgress + 1;
        TLoadingView.ShowProgress(TLoadingView.nProgress);
        ]]
        if (32 == nFrame) and (nil ~= TLoadingView.pLoadView) then
            TLoadingView.pLoadView:EndAnimate();
            ui_utility.setproperty_by_cid(TLoadingView.pLoadView:GetControlID(), 
                                          "Image", ELoadingView.sLoadAni);
            TLoadingView.pLoadView:StartAnimate();
        end;
    end;

    -- 加载界面
    dwRootID  = 0;
    pPnlView  = nil;

    nProgress = 0;            -- 动画进度
    bStartTmr = false;        -- 定时器是否开启

    -- 安全检测
    pChkView  = nil;
    pLblIcon  = nil;        -- Icon
    pLvCheck  = nil;        -- 检测进度

    -- 加载界面
    pLoadView = nil;

    -- 状态
    pLblState = nil;

};